package com.shiro.controller;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller

public class ShiroController {
    private static final transient Logger log = LoggerFactory.getLogger(Class.class);

    @Resource
    private SecurityManager securityManager;

    @RequestMapping("/login")
    @ResponseBody
    public void login(String name,String password){
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(name, password);
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            System.out.println("用户名不存在:" + token.getPrincipal());
        } catch (IncorrectCredentialsException ice) {
            System.out.println("账户密码 " + token.getPrincipal()  + " 不正确!");
        } catch (LockedAccountException lae) {
            System.out.println("用户名 " + token.getPrincipal() + " 被锁定 !");
        }

    }
    @RequestMapping("/user")
    @ResponseBody
    public Subject user(){
        Subject currentUser = SecurityUtils.getSubject();

        System.out.println(currentUser.isAuthenticated());
        //say who they are:
        //print their identifying principal (in this case, a username):
        log.info("用户 " + currentUser.getPrincipal() + " 登陆成功！");


        //test a role:
        if (currentUser.hasRole("管理员")) {
            log.info("管理员!");
        } else {
            log.info("user用户");
        }

        //test a typed permission (not instance-level)
        if (currentUser.isPermitted("article:list")) {
            log.info("新增");
        } else {
            log.info("没有有移动列表权限");
        }

        return currentUser;
    }
    @RequestMapping("/aaa")
    @ResponseBody
    @RequiresPermissions("article:list")
    public String aaa(){
        Subject currentUser = SecurityUtils.getSubject();

        System.out.println(currentUser.isAuthenticated());
        //say who they are:
        //print their identifying principal (in this case, a username):
        log.info("用户 " + currentUser.getPrincipal() + " 登陆成功！");


        //test a role:
        if (currentUser.hasRole("管理员")) {
            log.info("管理员!");
        } else {
            log.info("user用户");
        }

        //test a typed permission (not instance-level)
        if (currentUser.isPermitted("article:list")) {
            log.info("新增");
        } else {
            log.info("没有有移动列表权限");
        }

        return "ssssssssss";
    }
    @RequestMapping("/bbb")
    @ResponseBody
    @RequiresPermissions("assst")
    public String bbb(){
        Subject currentUser = SecurityUtils.getSubject();

        System.out.println(currentUser.isAuthenticated());

        return "bbbbbbbbbbb";
    }

}
